<!-- <script setup>
import { useUserStore } from "@/stores/useUserStore";
import { ElMessage } from "element-plus";
import router from "@/router";
import { ref, onMounted } from "vue";
const userStore = useUserStore();
const { login, userRef } = userStore;

const accountRef = ref("12345");
const passRef = ref("123");

onMounted(() => {
  // 如果已经登录了，在进入 登录页，则转发到 首页/仪表盘
  if (JSON.stringify(userRef) !== "{}") {
    router.push({ path: "/" });
  }
});

/**
 * 登录
 */
function loginIn() {
  if (accountRef.value.length <= 0 && passRef.value.length <= 0) {
    ElMessage.warning("账号密码输入完整");
  } else {
    login(accountRef.value, passRef.value).then(({ code, msg }) => {
      if (code === 2000) {
        ElMessage.success("登录成功");
        router.push({ path: "/" });
      } else {
        ElMessage.error(msg);
      }
    });
  }
}
</script>

<template>
  <div class="login-container">

    <el-form>
      <el-form-item label="学号">
        <el-input v-model="accountRef" />
      </el-form-item>
      <el-form-item label="密码">
        <el-input v-model="passRef" placeholder="默认123" />
      </el-form-item>
    </el-form>
    <el-button type="primary" @click="loginIn">登录</el-button>
  </div>
</template>

<style scoped></style> -->




<script setup>
import { ElMessage } from "element-plus";
import { User, Lock } from '@element-plus/icons-vue'
import { ref, watch, onMounted } from 'vue'
import router from "@/router/index";
import { loginUser, register } from '@/api/userApi';
import { useUserStore } from "@/stores/useUserStore";
const userStore = useUserStore();
const { login, userRef } = userStore;

onMounted(() => {
  // 如果已经登录了，在进入 登录页，则转发到 首页/仪表盘
  if (JSON.stringify(userRef) !== "{}") {
    router.push({ path: "/" });
  }
});

const isRegister = ref(true)
//注册的数据
const formModel = ref({
  username: '',
  password: '',
  // repassword: ''
  nickname: '',
})

// 配置校验的信息
const rules = {
  username: [
    { required: true, message: '请输入您的学号', trigger: 'blur' },
    { min: 5, max: 10, message: '学号必须是5-10位的字符', trigger: 'blur' }
  ],
  password: [
    { required: true, message: '请输入密码', trigger: 'blur' },
    {
      pattern: /^\S{3,10}$/,
      message: '密码必须是3-10位的非空字符',
      trigger: 'blur'
    }
  ],
  nickname: [
    { required: true, message: '请输入您的姓名', trigger: 'blur' },
    {
      pattern: /^\S{1,10}$/,
      message: '输入的姓名不能空',
      trigger: 'blur'
    },
    // {
    //   validator: (rule, value, callback) => {
    //     if (value !== formModel.value.password) {
    //       callback(new Error('两次输入密码不一致!'))
    //     } else {
    //       callback()
    //     }
    //   },
    //   trigger: 'blur'
    // }
  ]
}

//切换的时候重置
watch(isRegister, () => {
  formModel.value = {
    username: '',
    password: '',
    nickname: ''
  }
})


//注册的函数
//注册之前进行校验
const registerUser = async () => {

  console.log('开始注册请求')
  console.log()
  if (formModel.value.username.length <= 0 && formModel.value.password.length <= 0) {
    ElMessage.warning("账号密码输入不完整");
  } else {
    register(formModel.value.username, formModel.value.password, formModel.value.nickname).then(({ code, msg }) => {
      if (code === 2000) {
        ElMessage.success("注册成功，请您登录");
        isRegister.value = false
      }
    }).catch(err => {
      console.log(err);
    });
  }
  // 切换到登录
  // isRegister.value = false


}


//登录的函数
const loginUserFa = async () => {
  console.log(formModel.value.username)
  console.log('开始登录')

  if (formModel.value.username.length <= 0 && formModel.value.password.length <= 0) {
    ElMessage.warning("账号密码输入不完整");
  } else {
    login(formModel.value.username, formModel.value.password).then(({ code, msg }) => {
      if (code === 2000) {
        ElMessage.success("登录成功");
        router.push({ path: "/" });
      }
    });
  }
}

</script>

<template>
  <el-row class="login-page">
    <el-col :span="12" class="bg"></el-col>
    <el-col :span="6" :offset="3" class="form">

      <!-- 注册的表单 -->
      <el-form ref="form" size="large" autocomplete="off" v-if="isRegister" :model="formModel" :rules="rules">
        <el-form-item>
          <h1>注册</h1>
        </el-form-item>

        <el-form-item prop="username">
          <el-input v-model="formModel.username" :prefix-icon="User" placeholder="请输入用户名（学号）"></el-input>
        </el-form-item>

        <el-form-item prop="password">
          <el-input v-model="formModel.password" :prefix-icon="Lock" type="password" placeholder="请输入密码"></el-input>
        </el-form-item>

        <el-form-item prop="nickname">
          <el-input v-model="formModel.nickname" :prefix-icon="Lock" placeholder="请输入您的姓名"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button @click="registerUser" class="button" type="primary" auto-insert-space>
            注册
          </el-button>
        </el-form-item>
        <el-form-item class="flex">
          <el-link type="info" :underline="false" @click="isRegister = false">
            ← 返回
          </el-link>
        </el-form-item>
      </el-form>

      <!-- 登录的表单 -->
      <el-form ref="form" size="large" autocomplete="off" v-else :model="formModel" :rules="rules">
        <el-form-item>
          <h1>登录</h1>
        </el-form-item>

        <el-form-item prop="username">
          <el-input v-model="formModel.username" :prefix-icon="User" placeholder="请输入用户名"></el-input>
        </el-form-item>

        <el-form-item prop="password">
          <el-input v-model="formModel.password" name="password" :prefix-icon="Lock" type="password"
            placeholder="请输入密码"></el-input>
        </el-form-item>

        <el-form-item class="flex">
          <div class="flex">
            <el-checkbox>记住我</el-checkbox>
            <el-link type="primary" :underline="false">忘记密码？</el-link>
          </div>
        </el-form-item>

        <el-form-item>
          <el-button class="button" type="primary" auto-insert-space @click="loginUserFa">登录</el-button>
        </el-form-item>

        <el-form-item class="flex">
          <el-link type="info" :underline="false" @click="isRegister = true">
            注册 →
          </el-link>
        </el-form-item>
      </el-form>


    </el-col>
  </el-row>
</template>

<style lang="scss" scoped>
.login-page {
  height: 100vh;
  background-color: #fff;

  .bg {
    background: url('@/assets/logo3.png') no-repeat 60% center / 400px auto,
      url('@/assets/login_bg.jpg') no-repeat center / cover;
    border-radius: 0 20px 20px 0;
  }

  .form {
    display: flex;
    flex-direction: column;
    justify-content: center;
    user-select: none;

    .title {
      margin: 0 auto;
    }

    .button {
      width: 100%;
    }

    .flex {
      width: 100%;
      display: flex;
      justify-content: space-between;
    }
  }
}
</style>